<template>
	<div class="headerbox">
		<div class="header">
			<div class="headerL">
				<a @click="goBack" class="goback"><img src="@/assets/picture/goback.png" /></a>
			</div>
			<div class="headerC">
				<p>确认订单</p>
			</div>
			<div class="headerR"></div>
		</div>
	</div>
	<div class="hbox"></div>
	<div class="jsdingdan">
		<div v-for="(order, index) in productList" :key="index">
			<div class="jsxq">
				<div class="jsxq_1">
					<img src="@/assets/picture/22.png" />
				</div>
				<div class="jsxq_2">
					<div class="js_1">
						<p class="p1">{{order.name}}</p>
						<p class="p2">￥{{order.price}}</p>
					</div>
					<div class="js_2">
						<p class="p4">×{{order.buyNum}}</p>
					</div>
				</div>
			</div>
			<div class="jshj">
				<div class="jshjp">共 {{ order.buyNum }} 件商品<span class="sp1">合计：</span><span
						class="sp2">￥{{ order.totalPrice }}</span></div>
			</div>
		</div>
	</div>
	<div class="hejiBox jiesuan">
		<div class="heji">
			<div class="heji_3">
				<p>总计：<span>￥{{ totalPriceSum }}</span></p>
			</div>
			<div class="heji_5">
				<a @click="pay()">确认</a>
			</div>
		</div>
	</div>
	<div v-if="tip" class="tip-layer">
		<div class="tip">{{tipContent}}</div>
	</div>
</template>
<script>
	import axios from 'axios';
	import {
		showTips
	} from '@/assets/js/common.js';
	export default {
		name: 'xq',
		props: {
			msg: String
		},
		data() {
			return {
				httpUrl: 'http://qgm1.fengjian123.com/service9501/',
				tipContent: '',
				tip: 0,
				productList: '',
				totalPriceSum: 0,
				userData: '',
				is_sale: 0,
				is_now: 0,
				cartList: [],
			};
		},
		mounted() {
			this.getDetail();
		},
		methods: {
			async getDetail() {
				const sessionData = window.sessionStorage.getItem('users');
				if (sessionData == null) {
					this.$router.push('/login');
					console.log("登录失效");
					return;
				}

				this.userData = JSON.parse(sessionData);
				const productData = window.sessionStorage.getItem('productData_' + this.userData.id);
				if (!productData) {
					this.$router.go(-1);
					return;
				}

				const productDatas = JSON.parse(productData);
				if (productDatas.productList.length == 0) {
					this.$router.go(-1);
					return;
				}

				this.is_sale = productDatas['is_sale'];
				this.is_now = productDatas['is_now'];
				const productLists = productDatas.productList;
				let productArr = [];
				for (let i = 0; i < productLists.length; i++) {
					productArr.push(productLists[i]['productId']);
				}

				this.cartList = JSON.parse(window.sessionStorage.getItem('cart_' + this.userData.id));

				axios.post(this.httpUrl + 'shop/getOrderdetail', null, {
						headers: {
							'Authorization': 'Bearer ' + this.userData.token,
							'Content-Type': 'application/json',
						},
						params: {
							productArr: JSON.stringify(productArr),
							is_sale: productDatas['is_sale']
						}
					})
					.then(res => {
						if (res.data.err == 0) {
							const productList = res.data.productList.map((item, index) => {
								const totalPrice = item.price * productLists[index].buyNum;
								return {
									...item,
									...productLists[index],
									totalPrice
								};
							});

							this.productList = productList;
							this.totalPriceSum = productList.reduce((sum, item) => sum + item.totalPrice, 0);
						} else {
							console.error("获取订单详情失败，请检查网络", res.data.msg);
							showTips(this, '获取订单详情失败，请检查网络');
						}
					})
					.catch(error => {
						console.error("登录失败，请检查网络", error);
						showTips(this, '登录失败，请检查网络');
					});
			},
			async pay() {
				const payProduct = this.productList.map(product => ({
					'product_id': product.productId,
					'num': product.buyNum
				}));

				let buyStyle;
				if (this.is_sale == 1) {
					buyStyle = 'shop/buySpike';
				} else {
					buyStyle = 'shop/buyNormal';
				}

				axios.post(this.httpUrl + buyStyle, null, {
						headers: {
							'Authorization': 'Bearer ' + this.userData.token,
							'Content-Type': 'application/json',
						},
						params: {
							userid: this.userData.id,
							shopData: JSON.stringify(payProduct)
						}
					})
					.then(res => {
						if (res.data.err == 0) {
							showTips(this, '购物成功');

							if (this.cartList && this.is_now == 0) {  // 购买成功后，是否删除购物车记录
								let ProductIds = payProduct.map(item => item.product_id);
								this.cartList = this.cartList.filter(item => !ProductIds.includes(item.productId));
								sessionStorage.setItem('cart_' + this.userData.id, JSON.stringify(this.cartList));
							}

							setTimeout(() => {
								this.$router.push('/allDd');
							}, 1000);
						} else {
							console.error(res.data.msg);
							showTips(this, res.data.msg);
						}
					})
					.catch(error => {
						console.error("登录失败，请检查网络", error);
						showTips(this, '登录失败，请检查网络');
					});
			},
			goBack() {
				this.$router.go(-1);
			},
		},
	}
</script>

<style>
	@import url('@/assets/css/style.css');
</style>